package com.lwj.liwanjia.view.activity.myindent;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.indent.ReturnAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.CancleBean;
import com.lwj.liwanjia.bean.MyIndentBean;
import com.lwj.liwanjia.impl.CancleView;
import com.lwj.liwanjia.impl.MyIndentView;
import com.lwj.liwanjia.presenter.CancleTkPresenter;
import com.lwj.liwanjia.presenter.MyIndentPresenter;
import com.lwj.liwanjia.utils.CancleDialog;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo.ApplyStatusActivity;
import com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo.TxWlActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.ding_dan.WuLiuActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ReturnActivity extends BaseActivity implements MyIndentView, CancleView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rlv_return)
    SuperRecyclerView rlvReturn;
    @BindView(R.id.progress)
    ProgressBar progressBar;
    @BindView(R.id.no_indent)
    TextView noIndent;

    private ReturnAdapter adapter;
    private List<MyIndentBean.DataBean> list = new ArrayList<>();
    private MyIndentPresenter presenter;
    private String token;
    private CancleTkPresenter tkPresenter;//取消退款申请
    private int canclePosition;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("退货退款");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rlvReturn.setLayoutManager(manager);
        rlvReturn.setRefreshEnabled(false);
        rlvReturn.setLoadMoreEnabled(false);
        adapter = new ReturnAdapter(this, list);
        rlvReturn.setAdapter(adapter);
        presenter = new MyIndentPresenter(this);
        tkPresenter = new CancleTkPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
        listener();
    }

    private void listener() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(ReturnActivity.this, ApplyStatusActivity.class);
                intent.putExtra("tradeid", list.get(position).getGoods().getTradeId());
                intent.putExtra("phone", (String) list.get(position).getGoods().getShoptel());
                startActivity(intent);
            }
        });
        adapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, final int position) {
                canclePosition = position;
                TextView tv = (TextView) view;
                if (tv.getText().toString().equals("退款详情")) {
                    Intent intent = new Intent(ReturnActivity.this, ApplyStatusActivity.class);
                    intent.putExtra("tradeid", list.get(position).getGoods().getTradeId());
                    intent.putExtra("phone", (String) list.get(position).getGoods().getShoptel());
                    startActivity(intent);
                } else if (tv.getText().toString().equals("填写物流")) {
                    Intent intent = new Intent(ReturnActivity.this, TxWlActivity.class);
                    intent.putExtra("data", list.get(position).getGoods());
                    startActivity(intent);
                } else if (tv.getText().toString().equals("联系商家")) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + list.get(position).getGoods().getShoptel()));
                    startActivity(intent);
                } else if (tv.getText().toString().equals("取消申请")) {
                    final CancleDialog dialog = new CancleDialog(ReturnActivity.this, "退款");
                    dialog.show();
                    dialog.getSure().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            tkPresenter.cacleTk(token, list.get(position).getGoods().getTradeId());
                            dialog.dismiss();
                        }
                    });
                } else if (tv.getText().toString().equals("查看物流")) {
                    Intent intent = new Intent(ReturnActivity.this, WuLiuActivity.class);
                    intent.putExtra("dingDanHao",list.get(position).getGoods().getTradeId());
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getIndentData(token, "退货退款");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_return;
    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void cancleTk(CancleBean cancleBean) {
        if (cancleBean.getState() == 0) {
            Toast.makeText(this, "取消申请失败", Toast.LENGTH_SHORT).show();
        } else if (cancleBean.getState() == 1) {
            if (cancleBean.getData().getStatus().equals("取消申请成功")) {
                list.remove(canclePosition);
                if (list.size() == 0) {
                    noIndent.setVisibility(View.VISIBLE);
                    rlvReturn.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
                Toast.makeText(this, "取消申请成功！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void getIndentData(MyIndentBean myIndentBean) {
        if (myIndentBean.getState() == 0) {
            Toast.makeText(this, "访问失败", Toast.LENGTH_SHORT).show();
        } else if (myIndentBean.getState() == 1) {
            list.clear();
            list.addAll(myIndentBean.getData());
            if (list.size() == 0) {
                noIndent.setVisibility(View.VISIBLE);
                rlvReturn.setVisibility(View.GONE);
            } else {
                noIndent.setVisibility(View.GONE);
                rlvReturn.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter = null;
        }
        if (tkPresenter != null) {
            tkPresenter = null;
        }
    }

}
